package edu.unindra.raport.gui;

import javax.swing.AbstractAction;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.SwingWorker;

import edu.unindra.raport.template.About;
import edu.unindra.raport.template.PanelBackGround;
import edu.unindra.raport.template.Toolbar;
import usu.widget.ButtonGlass;
import usu.widget.Form;
import usu.widget.GlassPane;

import java.awt.AWTException;
import java.awt.CardLayout;
import java.awt.Toolkit;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import usu.widget.constan.Animation;
import usu.widget.util.WidgetUtilities;

import java.awt.Dimension;
import javax.swing.ImageIcon;

public class FormMenu extends Form {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2471410853205547369L;

	private ExecutorService executor;
	private Toolkit toolkit;
	private GlassPane glassPane;
	private PanelBackGround mainPanel;
	private JPanel mainSubPanel;

	private Toolbar toolBar;
	private JLabel titleBody;
	private About about;
	private ButtonGlass btnHome;

	private FormDetailMenu formDetailMenu;
	private FormDetailMenuHome formDetailMenuHome;
	private FormDetailMenuOperator formDetailMenuOperator;
	private FormDetailMenuSiswa formDetailMenuSiswa;
	private FormDetailMenuGuru formDetailMenuGuru;
	private FormDetailMenuNilai formDetailMenuNilai;

	/** Creates new form ELearningForm */
	public FormMenu() {
		setResizable(false);
		executor = Executors.newCachedThreadPool();
		toolkit = Toolkit.getDefaultToolkit();

		getContentPane().setEnabled(false);
		setAnimationShow(Animation.SHOW_FROM_BOTTOM);
		setAnimationHide(Animation.HIDE_TO_BOTTOM);
		setLocationRelativeTo(null);

		setMinimumSize(new Dimension(750, 640));

		java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit()
				.getScreenSize();
		setBounds((screenSize.width - 800) / 2, (screenSize.height - 600) / 2,
				800, 600);

		addWindowListener(new java.awt.event.WindowAdapter() {
			public void windowClosing(java.awt.event.WindowEvent evt) {
				formWindowClosing(evt);
			}
		});

		initComponents();

		getRootPane().getInputMap().put(
				KeyStroke.getKeyStroke((char) KeyEvent.VK_ESCAPE), "CLOSE");
		getRootPane().getActionMap().put("CLOSE", new AbstractAction() {

			private static final long serialVersionUID = -1;

			public void actionPerformed(ActionEvent e) {
				((GlassPane) getGlassPane()).hideComponent();
			}
		});

		initActions();

		showPanel(formDetailMenuHome, "HOME");
	}

	private void initComponents() {
		glassPane = new usu.widget.GlassPane();
		mainPanel = new PanelBackGround();
		mainSubPanel = new JPanel();

		formDetailMenu = new FormDetailMenu();
		formDetailMenu.getBtnGuru().setText("GURU");
		formDetailMenu.getBtnSiswa().setText("SISWA");
		formDetailMenu.getBtnNilai().setText("NILAI");
		formDetailMenu.getBtnReport().setText("LAPORAN");
		formDetailMenu.getBtnHome().setText("HOME");
		formDetailMenu.getBtnOperator().setText("OPERATOR");

		formDetailMenuHome = new FormDetailMenuHome();
		formDetailMenuOperator = new FormDetailMenuOperator();
		formDetailMenuSiswa = new FormDetailMenuSiswa();
		formDetailMenuGuru = new FormDetailMenuGuru();
		formDetailMenuNilai = new FormDetailMenuNilai();

		about = new About();
		about.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				aboutActionPerformed(evt);
			}
		});

		toolBar = new Toolbar();
		toolBar.setRollover(true);
		toolBar.setToolTipText("Tool Bar");

		btnHome = new ButtonGlass();
		btnHome.setFont(new Font("Tahoma", Font.BOLD, 11));
		btnHome.setForeground(new java.awt.Color(255, 255, 255));
		btnHome.setIcon(new ImageIcon(FormMenu.class
				.getResource("/edu/unindra/raport/image/Home 2.png"))); // NOI18N
		btnHome.setMnemonic('M');
		btnHome.setText("Menu");
		btnHome.setToolTipText("Klik untuk kembali ke menu utama");
		btnHome.setFocusable(false);
		btnHome.setRoundRect(true);
		btnHome.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
		btnHome.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				buttonHomeActionPerformed(evt);
			}
		});
		toolBar.add(btnHome);

		titleBody = new JLabel();
		titleBody.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
		titleBody.setForeground(new java.awt.Color(255, 255, 255));
		titleBody.setText("Raport Aplikasi");

		mainSubPanel.setOpaque(false);
		mainSubPanel.setLayout(new java.awt.CardLayout());
		mainSubPanel.add(formDetailMenu, "MENU");
		mainSubPanel.add(formDetailMenuHome, "HOME");
		mainSubPanel.add(formDetailMenuOperator, "OPERATOR");
		mainSubPanel.add(formDetailMenuSiswa, "SISWA");
		mainSubPanel.add(formDetailMenuGuru, "GURU");
		mainSubPanel.add(formDetailMenuNilai, "NILAI");

		setGlassPane(glassPane);
		glassPane.setAlphaDisable(10);
		glassPane.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.CENTER,
				100, 100));

		javax.swing.GroupLayout panelUtamaLayout = new javax.swing.GroupLayout(
				mainPanel);
		mainPanel.setLayout(panelUtamaLayout);
		panelUtamaLayout
				.setHorizontalGroup(panelUtamaLayout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								panelUtamaLayout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												panelUtamaLayout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addComponent(
																mainSubPanel,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																764,
																Short.MAX_VALUE)
														.addComponent(titleBody))
										.addContainerGap()));
		panelUtamaLayout
				.setVerticalGroup(panelUtamaLayout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								panelUtamaLayout
										.createSequentialGroup()
										.addContainerGap()
										.addComponent(titleBody)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
										.addComponent(
												mainSubPanel,
												javax.swing.GroupLayout.DEFAULT_SIZE,
												451, Short.MAX_VALUE)
										.addContainerGap()));

		getContentPane().add(mainPanel, java.awt.BorderLayout.CENTER);
		getContentPane().add(toolBar, java.awt.BorderLayout.PAGE_START);

	}

	private void initActions() {
		formDetailMenu.getBtnHome().addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				if (formDetailMenuHome.isVisible()) {
					return;
				}

				showPanel(formDetailMenuHome, "HOME");
				setTitleBody("HOME");
			}
		});

		formDetailMenu.getBtnOperator().addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				if (formDetailMenuOperator.isVisible()) {
					return;
				}

				showPanel(formDetailMenuOperator, "OPERATOR");
				setTitleBody("OPERATOR");
			}
		});

		formDetailMenu.getBtnSiswa().addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				if (formDetailMenuSiswa.isVisible()) {
					return;
				}

				showPanel(formDetailMenuSiswa, "SISWA");
				setTitleBody("SISWA");

			}
		});

		formDetailMenu.getBtnGuru().addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				if (formDetailMenuGuru.isVisible()) {
					return;
				}

				showPanel(formDetailMenuGuru, "GURU");
				setTitleBody("GURU");

			}
		});

		formDetailMenu.getBtnNilai().addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				if (formDetailMenuNilai.isVisible()) {
					return;
				}

				showPanel(formDetailMenuNilai, "NILAI");
				setTitleBody("NILAI");

			}
		});

	}

	protected void buttonDeleteElearningFormMianAdminOperator(ActionEvent e) {

	}

	protected void buttonEditElearningFormMainAdminOperator(ActionEvent e) {
	}

	private void aboutActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_aboutActionPerformed
		((GlassPane) getGlassPane()).hideComponent();
	}

	private void buttonHomeActionPerformed(java.awt.event.ActionEvent evt) {
		if (formDetailMenu.isVisible()) {
			return;
		}
		showTransition(formDetailMenu);
		setTitleBody(null);
		((CardLayout) mainSubPanel.getLayout()).show(mainSubPanel, "MENU");

	}

	private void showTransition(JComponent com) {
		try {
			((GlassPane) getGlassPane()).startTransition(com);
		} catch (AWTException ex) {
		}
	}

	public void showPanel(JPanel panel, String card) {
		if (panel.isVisible()) {
			return;
		} else {
			showTransition(mainPanel);
			((CardLayout) mainSubPanel.getLayout()).show(mainSubPanel, card);
		}
	}

	private void formWindowClosing(java.awt.event.WindowEvent evt) {// GEN-FIRST:event_formWindowClosing
		exit();
	}

	@Override
	public void exit() {
		if (showConfirm("Anda Yakin?") != JOptionPane.OK_OPTION) {
			return;
		}
		executor.shutdown();
		super.exit();
	}

	private int showConfirm(String message) {
		return JOptionPane.showConfirmDialog(this, message, "Konfirmasi",
				JOptionPane.OK_CANCEL_OPTION);
	}

	public void setTitleBody(String title) {
		if (title == null) {
			titleBody.setText("Raport");
		} else {
			titleBody.setText(title);
		}
	}

	private void hideComponentFromGlasspane(ActionEvent e) {
		((GlassPane) getGlassPane()).hideComponent();
	}

	private void showComponentInGlasspane(JComponent comp) {
		((GlassPane) getGlassPane()).showComponent(comp);
	}

	private void buttonSubmitAddOperator(ActionEvent e) {
	}

	protected void buttonSubmitEditOperator(ActionEvent e) {
	}

	// public class LoadOperator extends SwingWorker<List<Operator>, String> {
	//
	// @Override
	// protected List<Operator> doInBackground() throws Exception {
	// return null;
	// }
	//
	// }

}
